package ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;

public class LowBatWarning extends Dialog {

	protected Object result;
	protected Shell shell;
	
	private boolean bExitFlag = false;
	private Shell parent_shell;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public LowBatWarning(Shell parent, int style) {
		super(parent, style);
		setText("Warning!");
		parent_shell = parent;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay(); 		
		Point location = parent_shell.getLocation();		// Get the location of the parent. *SR
		shell.setLocation(location.x+68, location.y+26);
		
		Label label = new Label(shell, SWT.NONE);
		label.setText("WARNING:");
		label.setFont(SWTResourceManager.getFont("Times New Roman", 35, SWT.NORMAL));
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		label.setAlignment(SWT.CENTER);
		label.setBounds(9, 10, 335, 53);
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setText("Low Battery");
		label_1.setFont(SWTResourceManager.getFont("Times New Roman", 35, SWT.BOLD));
		label_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label_1.setAlignment(SWT.CENTER);
		label_1.setBounds(10, 79, 335, 53);
		
		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setText("Please connect to charger to prevent loss of services.");
		label_2.setFont(SWTResourceManager.getFont("Times New Roman", 16, SWT.NORMAL));
		label_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label_2.setAlignment(SWT.CENTER);
		label_2.setBounds(10, 141, 335, 53);
		while (!shell.isDisposed() && bExitFlag == false) { // *SR
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		shell.dispose(); // *SR
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
		shell.setSize(360, 284);
		shell.setText(getText());
		
		Button btnOk = new Button(shell, SWT.NONE);
		btnOk.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				bExitFlag = true; // *SR
			}
		});
		btnOk.setFont(SWTResourceManager.getFont("Times New Roman TUR", 24, SWT.NORMAL));
		btnOk.setBounds(232, 216, 112, 56);
		btnOk.setText("OK");

	}
}
